package com.wanmait.gohome.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

    public static String getCookie(String name, HttpServletRequest request)
    {
        String value = null;
        Cookie[] cookies = request.getCookies();
        if(cookies!=null)
        {
            for(Cookie cookie : cookies)
            {
                if(cookie.getName().equals(name))
                {
                    value = cookie.getValue();
                    break;
                }
            }
        }
        return value;
    }

    public static void addCookie(String name, String value, String path, int maxAge, HttpServletResponse response)
    {
        Cookie cookie = new Cookie(name,value);
        //        //2 设置起作用路径
        cookie.setPath(path);
        //3 设置生命周期
        cookie.setMaxAge(maxAge);
        //4 发送cookie
        response.addCookie(cookie);

    }
}
